﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rotempco.Website.Objects;
using Rotempco.BaseClass.Objects.Enums;
using Rotempco.Website.Logic;
using Rotempco.BaseClass;
using Rotempco.Core.Logic;

namespace Rotempco.Website.MasterPages
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        public bool PrintPage { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBindControls();

            if (PrintPage)
            {
                cssStylesheet.Attributes.Add("href", "/CSS/Print.css");
            }
            else
            {
                cssStylesheet.Attributes.Add("href", "/CSS/Admin.css");
            }
        }

        private void DataBindControls()
        {
            Literal_LoginName.Text = SessionStore.Instance.LoginUser.Name;
            Literal_LoginRole.Text = "Administator";
            ucMenuAdministrator.Visible = true;
            Literal_MenuRole.Text = "Administrator";
            Literal_BreadCrumbRole.Text = "Admin";
        }

        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                // TODO: Remove autologin
                AutoLoginLogic.Login();

                if (SessionStore.Instance.LoginUser == null)
                {
                    Response.Redirect("/");
                }
                else if (SessionStore.Instance.LoginUser.ChangePasswordOnNextLogin == true && !Request.Url.AbsolutePath.ToLower().Contains("/admin/changepassword/default.aspx"))
                {
                    Response.Redirect("/Admin/ChangePassword/Default.aspx");
                }
                else
                {
                    CoreSettingsLogic.SetPrivateDecryptionKey(SessionStore.Instance.PrivateEncryptionKey);
                }
            }

            base.OnInit(e);
        }
    }
}